// main game controller
function EggCatcher(containerId){
	this.containerId = containerId;
	this.container = document.getElementById(containerId);
	this.size = {
		width : $(this.container).width(),
		height : $(this.container).height(),
	};
	this.backScene = null;
	this.frontScene = null;

	this.missedEggs = 0;
	this.caughtEggs = 0;
	
	this.init = function(){
		var self = this;
		// set container as relative
		$(this.container).empty();
		$(this.container).css("position","relative");

		// add the canvases
		var canvas = document.createElement("CANVAS");
		canvas.id = "front-scene";
		canvas.width = this.size.width;
		canvas.height = this.size.height;
		canvas.style.border = "2px solid black";
		canvas.style.position = "absolute";
		canvas.style.top = "0px";
		canvas.style.left = "0px";
		canvas.style.zIndex = 2;
		$(this.container).append(canvas);
		canvas.style.background = "url('./egg-catcher/images/background.png')";
		canvas.style.backgroundSize = "100% 100%";
		/*canvas = document.createElement("CANVAS");
		canvas.id = "back-scene";
		canvas.width = this.size.width;
		canvas.height = this.size.height+2;
		canvas.style.position = "absolute";
		canvas.style.top = "0px";
		canvas.style.left = "2px";
		canvas.style.background = "url('./images/background.png')";
		canvas.style.backgroundSize = "100% 100%";
		canvas.style.zIndex = 1;
		$(this.container).append(canvas);*/

		
		setTimeout(function(){
			//self.backScene = new Scene("back-scene");
			self.frontScene = new Scene("front-scene");
			self.backScene = self.frontScene;
			self.startGame();
		},1);		
	}

	this.init();

	this.cloudsController = null;

	this.startGame = function(){
		this.cloudsController = new CloudsController(this.backScene);
		this.cloudsController.start();

		this.initElems();
	}

	this.catcher = null;
	this.catcherAnim = null;
	this.eggInterval = null;
	this.initElems = function(){
		var self = this;

		self.catcherLeftSrc = "./egg-catcher/images/catcher-left.png";
		self.catcherRightSrc = "./egg-catcher/images/catcher-right.png";
		var image = new Image();
		image.onload = function(){
		
			var height = self.size.height * 0.4;
			var width = height / this.height * this.width;
			var top = self.size.height - height;
			var left = (self.size.width - width)/2;

			
			self.catcher = new CanvasImage(left, top, width, height, self.catcherRightSrc);
			self.frontScene.add(self.catcher);			
			self.frontScene.invalidate();

			var draggable = new Draggable();
			draggable.setDraggable(self.catcher);

			self.catcherAnim =  new Animation(self.catcher, {mode:"speed"});
			self.catcherAnim.precision = 2;
		}; 

		image.src = self.catcherRightSrc;

		var domUtils = new DomUtils();
		var moveSpeed = 550;
	
		domUtils.addEventSimple(window, "keydown", function(e){
			if(self.computing)return;
			self.computing = true;
			var evt=(e)?e:(window.event)?window.event:null;
			if(evt){
				var key=(evt.charCode)?evt.charCode:((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));				
				if(key==37){
					//move left
					if(!self.catcherAnim.movingLeft){						
						self.catcherAnim.stop();
						self.catcherAnim.movingRight = false;
						self.catcherAnim.movingLeft = false;

						self.catcherAnim =  new Animation(self.catcher, {mode:"speed"});
						self.catcherAnim.movingRight = false;
						self.catcherAnim.movingLeft = true;

						self.catcher.changeSource(self.catcherLeftSrc);

						self.catcherAnim.animateMove(0,self.catcher.y,moveSpeed,null,null);
					}
				}
				if(key==39){
					//move right
					if(!self.catcherAnim.movingRight){
						self.catcherAnim.stop();
						self.catcherAnim.movingRight = false;
						self.catcherAnim.movingLeft = false;

						self.catcherAnim =  new Animation(self.catcher, {mode:"speed"});
						self.catcherAnim.movingRight = true;
						self.catcherAnim.movingLeft = false;

						self.catcher.changeSource(self.catcherRightSrc);

						self.catcherAnim.animateMove(self.size.width-self.catcher.w,self.catcher.y,moveSpeed,null,null);
					}
				}
			}
			self.computing = false;
		});
		domUtils.addEventSimple(window, "keyup", function(e){
			self.catcherAnim.movingRight = false;
			self.catcherAnim.movingLeft = false;
			self.catcherAnim.stop();
		});

		this.eggInterval = setInterval(function(){
			
		    var eggSrc = "./egg-catcher/images/egg.png";
			var image=  new Image();
			image.onload = function(){
				var width = self.size.width * 0.03;
				var height = width/this.width * this.height;
				var left =  Math.floor((Math.random() * self.size.width-width) + width);
				var top = 0;

				var egg = new CanvasImage(left, top, width, height, this.src);
				self.frontScene.add(egg);
				self.frontScene.invalidate();

				var anim = new Animation(egg, {mode:"speed"});
				var baseSpeed = 150;
				var speed = baseSpeed + (self.caughtEggs * 10);
				anim.animateMove(left,self.size.height+height,speed,function(percent){
					if(domUtils.boundariesIntersect(egg.boundary, self.catcher.boundary)){
						// egg was caught
						anim.stop();
						self.frontScene.remove(egg);
						self.frontScene.invalidate();
						self.caughtEggs++;
						if(typeof(self.onEggCaught)=="function")self.onEggCaught();
					}
				},function(){
					self.frontScene.remove(egg);
					self.frontScene.invalidate();
					self.missedEggs++;
					if(typeof(self.onEggMissed)=="function")self.onEggMissed();						
				});
			};
			image.src = eggSrc;

		},2000);
	}
}

// controls the coulds moving in the sky
function CloudsController(scene){
	this.scene = scene;
	this.clouds = ["./egg-catcher/images/cloud1.png", "./egg-catcher/images/cloud2.png", "./egg-catcher/images/cloud3.png"];
	this.intervalTime = 11000;
	this.interval = null;
	this.height = $(this.scene.canvas).height() * 0.3;
	this.width = $(this.scene.canvas).width();

	this.start = function(){
		var self = this;

		self.startCloud();
		this.interval = setInterval(function(){
			self.startCloud();		
		},this.intervalTime);

	}

	this.startCloud = function(){
		var self = this;

		var random = Math.floor((Math.random() * self.clouds.length-1) + 1);
		var cloudSrc = self.clouds[random];
		
		var image = new Image();
		image.onload = function(){
			var newWidth = 0.25 * self.width;
			var newHeight = newWidth/this.width * this.height;
			var img = new CanvasImage();
			var maxTop = self.height - newHeight;
			var newTop = Math.floor((Math.random() * maxTop) + 1);
			var newLeft = self.width;
			
			var img = new CanvasImage(newLeft, newTop, newWidth, newHeight, cloudSrc);
			scene.add(img);

			var anim = new Animation(img, {mode:"speed"});
			anim.animateMove(-newWidth,newTop,50, function(percent){
				//console.log(percent);				
			}, function(){	
				self.scene.remove(img);
			});
		};

		image.src = cloudSrc;			
	}
}